package java_0629;



//问题：用一个线程去终止另一个线程
//方法一：
public class ThreadDemo10 {
    private static  boolean flag = true;
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(){
            @Override
            public void run() {
                while(flag){
                    System.out.println("线程运行中.....");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                System.out.println("线程结束");
            }
        };
        thread.start();
        Thread.sleep(7000);
        flag=false;
    }

//方法二：
    public static void main2(String[] args) throws InterruptedException {
        Thread thread = new Thread(){
            @Override
            public void run() {
                while(!Thread.currentThread().isInterrupted()){
                    System.out.println("hello world!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        };
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();
    }
}
